package connect4swt.gui;

import com.swtdesigner.*;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.*;


import connect4swt.ClientApplication;


import java.util.List;

public class InviteOpponentWindow extends Dialog {

	private Tree treeGames;
	private Object result;
	private Shell invitingAnOpponentShell;
	
	private String destinationLogin;

	public InviteOpponentWindow(Shell parent, int style,String destinationLogin) {
		super(parent, style);
		this.destinationLogin = destinationLogin;
	}
	public InviteOpponentWindow(Shell parent,String destinationLogin) {
		super(parent, SWT.NONE);
		this.destinationLogin = destinationLogin;
	}


	public Object open(List<String> gameNames) {
		createContents();
		//Just testing tree filling
		fillGameTree(gameNames);
		invitingAnOpponentShell.open();
		invitingAnOpponentShell.layout();
		Display display = getParent().getDisplay();
		while (!invitingAnOpponentShell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	protected void createContents() {
		invitingAnOpponentShell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		invitingAnOpponentShell.setImage(SWTResourceManager.getImage(InviteOpponentWindow.class, "/images/newGame.png"));
		invitingAnOpponentShell.setSize(270, 235);
		invitingAnOpponentShell.setText("Inviting an opponent");

		final Label chooseAGameLabel = new Label(invitingAnOpponentShell, SWT.NONE);
		chooseAGameLabel.setFont(SWTResourceManager.getFont("Verdana", 12, SWT.BOLD));
		chooseAGameLabel.setText("Choose a game:");
		chooseAGameLabel.setBounds(10, 10, 472, 19);

		treeGames = new Tree(invitingAnOpponentShell, SWT.BORDER);
		
		treeGames.setBounds(10, 35, 242, 130);

		final Button btnOK = new Button(invitingAnOpponentShell, SWT.NONE);
		btnOK.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {								
				ClientApplication.getController().getGUIMediator().sendInvitationMessage(destinationLogin,treeGames.getSelection()[0].getText());			
				System.out.println("JOGO SELECCIONADO PARA CONVITE: "+treeGames.getSelection()[0].getText());						
				invitingAnOpponentShell.dispose();
			}
		});
		btnOK.setText("OK");
		btnOK.setBounds(161, 171, 44, 23);

		final Button btnCancel = new Button(invitingAnOpponentShell, SWT.NONE);
		btnCancel.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				invitingAnOpponentShell.dispose();
			}
		});
		btnCancel.setText("Cancel");
		btnCancel.setBounds(211, 171, 44, 23);
	}
	
	
	private void fillGameTree(List<String> gameNames) {
		TreeItem tempTreeItem;
		for(String gameName : gameNames) {
			tempTreeItem = new TreeItem(treeGames, SWT.NONE);
			tempTreeItem.setImage(SWTResourceManager.getImage(ClientWindow.class, "/images/newGame.png"));
			tempTreeItem.setText(gameName);
		}
	}
}
